﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PNunitToolsClass;

namespace PNunitLauncherConfigurationEditor
{
    public partial class Form1 : Form
    {
        private LauncherConfig config;

        public Form1()
        {
            InitializeComponent();
        }

        private void button_load_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "conf files (*.conf)|*.conf";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    this.config = LauncherConfig.Load(openFileDialog.FileName);
                    MessageBox.Show("Load succeed!");
                    this.refresh_all();
                    groupBox_all.Enabled =
                        button_save.Enabled = true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(string.Format("Load failed! {0}", ex.Message));
                }
            }
        }

        private void refresh_all()
        {
            this.refresh_result_file_name();
            this.refresh_machine_alias();
            this.refresh_launcher_machine();
            this.refresh_valiables();
            this.refreshlist_ParallelTests();
        }

        private void refreshlist_ParallelTests()
        {
            listBox_paralleltest.Items.Clear();
            if (null != this.config.Config.ParallelTests)
            {
                if (this.config.Config.ParallelTests.Count() > 0)
                {
                    foreach (LauncherConfig.ParallelTest paralleltest in this.config.Config.ParallelTests)
                    {
                        listBox_paralleltest.Items.Add(paralleltest.Name);
                    }
                }
            }
        }

        private void refresh_machine_alias()
        {
            if (this.config.Config.MachineAlias == null)
            {
                checkBox_Machine_Alias.Checked = groupBox_machine_alias.Enabled = false;
            }
            else
            {
                checkBox_Machine_Alias.Checked = groupBox_machine_alias.Enabled = true;
                this.refreshlist_machine_alias();
            }
        }

        private void refreshlist_machine_alias()
        {
            listBox_machine_alias.Items.Clear();
            if (null != this.config.Config.MachineAlias)
            {
                if (this.config.Config.MachineAlias.Count() > 0)
                {
                    foreach (string alias in this.config.Config.MachineAlias)
                    {

                        listBox_machine_alias.Items.Add(alias);
                    }
                }
            }
            refreshcom_machine();
        }

        private void refresh_valiables()
        {
            if (this.config.Config.Variables == null)
            {
                checkBox_Variables.Checked = groupBox_variables.Enabled = false;
            }
            else
            {
                checkBox_Variables.Checked = groupBox_variables.Enabled = true;
                this.refreshlist_variable();
            }
        }

        private void refreshlist_variable()
        {
            listBox_variables.Items.Clear();
            if (null != this.config.Config.Variables)
            {
                if (this.config.Config.Variables.Count() > 0)
                {
                    foreach (LauncherConfig.Variable variable in this.config.Config.Variables)
                    {
                        listBox_variables.Items.Add(variable.Name);
                    }
                }
            }
            refreshcom_machine();
        }

        private void refresh_result_file_name()
        {
            if (string.IsNullOrEmpty(this.config.Config.ResultFileName))
            {
                checkBox_Result_File_Name.Checked = groupBox_result_File_name.Enabled = false;
            }
            else
            {
                checkBox_Result_File_Name.Checked = groupBox_result_File_name.Enabled = true;
                textBox_result_file_name.Text = this.config.Config.ResultFileName;
            }
        }

        private void refresh_launcher_machine()
        {
            if (string.IsNullOrEmpty(this.config.Config.LauncherMachine))
            {
                checkBox_Launcher_Machine.Checked = groupBox_launcher_machine.Enabled = false;
            }
            else
            {
                checkBox_Launcher_Machine.Checked = groupBox_launcher_machine.Enabled = true;
                textBox__launcher_machine_ip.Text = this.config.Config.LauncherMachine.Split(':')[0];
                textBox_launcher_machine_port.Text = this.config.Config.LauncherMachine.Split(':')[1];
            }
        }

        private void button_new_Click(object sender, EventArgs e)
        {
            this.config = new LauncherConfig(new LauncherConfig.TestGroup());
            this.refresh_all();
            groupBox_all.Enabled =
                button_save.Enabled = true;
        }

        private void button_save_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "conf files (*.conf)|*.conf";
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    this.config.Save(saveFileDialog.FileName);
                    MessageBox.Show("Save succeed!");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(string.Format("Save failed! {0}", ex.Message));
                }
            }
        }

        private void button_select_result_file_name_Click(object sender, EventArgs e)
        {
            SaveFileDialog savefiledialog = new SaveFileDialog();
            savefiledialog.Filter = "xml files (*.xml)|*.xml";
            if (savefiledialog.ShowDialog() == DialogResult.OK)
            {
                textBox_result_file_name.Text = savefiledialog.FileName;
            }
        }

        private void checkBox_Result_File_Name_CheckedChanged(object sender, EventArgs e)
        {
            groupBox_result_File_name.Enabled = checkBox_Result_File_Name.Checked;
        }

        private void checkBox_Launcher_Machine_CheckedChanged(object sender, EventArgs e)
        {
            groupBox_launcher_machine.Enabled = checkBox_Launcher_Machine.Checked;
        }

        private void checkBox_Variables_CheckedChanged(object sender, EventArgs e)
        {
            groupBox_variables.Enabled = checkBox_Variables.Checked;
        }

        private void listBox_variables_SelectedIndexChanged(object sender, EventArgs e)
        {
            int select = listBox_variables.SelectedIndex;
            if (select < 0)
            {
                textBox_variables_name.Text =
                    textBox_variables_value.Text = string.Empty;
                button_variables_up.Enabled =
                    button_variables_down.Enabled =
                    button_variables_delete.Enabled =
                    button_variables_edit.Enabled = false;
                button_variables_add.Enabled = true;
            }
            else
            {
                textBox_variables_name.Text = this.config.Config.Variables[select].Name;
                textBox_variables_value.Text = this.config.Config.Variables[select].Value;
                button_variables_up.Enabled =
                button_variables_down.Enabled =
                button_variables_delete.Enabled =
                button_variables_edit.Enabled =
            button_variables_add.Enabled = true;
                if (select == 0)
                {
                    button_variables_up.Enabled = false;
                }
                if (select == listBox_variables.Items.Count - 1)
                {
                    button_variables_down.Enabled = false;
                }
            }
        }

        private void button_variables_up_Click(object sender, EventArgs e)
        {
            int up = listBox_variables.SelectedIndex;
            if (up > 0)
            {
                int before = up - 1;
                string upstr = this.config.Config.Variables[up].ToString();
                string beforestr = this.config.Config.Variables[before].ToString();

                LauncherConfig.Variable[] temp = new LauncherConfig.Variable[] { };
                for (int i = 0; i < listBox_variables.Items.Count; i++)
                {
                    if (i == before)
                    {
                        temp = temp.Concat(new LauncherConfig.Variable[] { new LauncherConfig.Variable(upstr) }).ToArray();
                    }
                    else if (i == up)
                    {
                        temp = temp.Concat(new LauncherConfig.Variable[] { new LauncherConfig.Variable(beforestr) }).ToArray();
                    }
                    else
                    {
                        temp = temp.Concat(new LauncherConfig.Variable[] { this.config.Config.Variables[i] }).ToArray();
                    }
                }
                this.config.Config.Variables = temp;
                this.refreshlist_variable();
                this.listBox_variables.SelectedIndex = up - 1;
            }
        }

        private void button_variables_down_Click(object sender, EventArgs e)
        {
            int down = listBox_variables.SelectedIndex;
            if (down >= 0 && down < listBox_variables.Items.Count)
            {
                int after = down + 1;
                string downstr = this.config.Config.Variables[down].ToString();
                string afterstr = this.config.Config.Variables[after].ToString();

                LauncherConfig.Variable[] temp = new LauncherConfig.Variable[] { };
                for (int i = 0; i < listBox_variables.Items.Count; i++)
                {
                    if (i == after)
                    {
                        temp = temp.Concat(new LauncherConfig.Variable[] { new LauncherConfig.Variable(downstr) }).ToArray();
                    }
                    else if (i == down)
                    {
                        temp = temp.Concat(new LauncherConfig.Variable[] { new LauncherConfig.Variable(afterstr) }).ToArray();
                    }
                    else
                    {
                        temp = temp.Concat(new LauncherConfig.Variable[] { this.config.Config.Variables[i] }).ToArray();
                    }
                }
                this.config.Config.Variables = temp;
                this.refreshlist_variable();
                this.listBox_variables.SelectedIndex = down + 1;
            }
        }

        private void button_variables_add_Click(object sender, EventArgs e)
        {
            if (this.config.Config.Variables != null)
            {
                foreach (LauncherConfig.Variable item in this.config.Config.Variables)
                {
                    if (item.Name == textBox_variables_name.Text)
                    {
                        MessageBox.Show("Variable " + textBox_variables_name.Text + " exists!");
                        return;
                    }
                }
            }
            else
            {
                this.config.Config.Variables = new LauncherConfig.Variable[] { };
            }
            int si = listBox_variables.Items.Count;
            this.config.Config.Variables = this.config.Config.Variables.Concat(
                new LauncherConfig.Variable[] { new LauncherConfig.Variable(
                    textBox_variables_name.Text,
                    textBox_variables_value.Text)}
                ).ToArray();
            this.refreshlist_variable();
            this.listBox_variables.SelectedIndex = si;
        }

        private void button_variables_delete_Click(object sender, EventArgs e)
        {
            int delete = listBox_variables.SelectedIndex;
            if (delete >= 0)
            {
                LauncherConfig.Variable[] temp = new LauncherConfig.Variable[] { };
                for (int i = 0; i < listBox_variables.Items.Count; i++)
                {
                    if (i == delete)
                    {
                        continue;
                    }
                    temp = temp.Concat(new LauncherConfig.Variable[] { this.config.Config.Variables[i] }).ToArray();
                }
                this.config.Config.Variables = temp;
                this.refresh_valiables();
                if (listBox_variables.Items.Count > 0)
                {
                    this.listBox_variables.SelectedIndex = delete - 1 < 0 ? 0 : delete - 1;
                }
            }
        }

        private void button_variables_edit_Click(object sender, EventArgs e)
        {
            int edit = listBox_variables.SelectedIndex;
            if (edit >= 0)
            {
                for (int i = 0; i < config.Config.Variables.Count(); i++)
                {
                    if (i == edit)
                    {
                        continue;
                    }

                    if (config.Config.Variables[i].Name == textBox_variables_name.Text)
                    {
                        MessageBox.Show("Variable " + textBox_variables_name.Text + " exists!");
                        return;
                    }
                }

                this.config.Config.Variables.SetValue(new LauncherConfig.Variable(
                    textBox_variables_name.Text,
                    textBox_variables_value.Text),
                    edit
                    );
                this.refreshlist_variable();
                this.listBox_variables.SelectedIndex = edit;
            }
        }

        private void listBox_machine_alias_SelectedIndexChanged(object sender, EventArgs e)
        {
            int select = listBox_machine_alias.SelectedIndex;
            if (select < 0)
            {
                textBox_machine_alias.Text =
                    textBox_machine_alias_ip.Text =
                    textBox_machine_alias_port.Text = string.Empty;
                button_machine_alias_up.Enabled =
                    button_machine_alias_down.Enabled =
                    button_machine_alias_delete.Enabled =
                    button_machine_alias_edit.Enabled = false;
                button_machine_alias_add.Enabled = true;
            }
            else
            {
                textBox_machine_alias.Text = this.config.Config.MachineAlias[select].Split('=')[0];
                textBox_machine_alias_ip.Text = this.config.Config.MachineAlias[select].Split('=')[1].Split(':')[0];
                textBox_machine_alias_port.Text = this.config.Config.MachineAlias[select].Split('=')[1].Split(':')[1];
                button_machine_alias_up.Enabled =
                button_machine_alias_down.Enabled =
                button_machine_alias_delete.Enabled =
                button_machine_alias_edit.Enabled =
            button_machine_alias_add.Enabled = true;
                if (select == 0)
                {
                    button_machine_alias_up.Enabled = false;
                }
                if (select == listBox_machine_alias.Items.Count - 1)
                {
                    button_machine_alias_down.Enabled = false;
                }
            }
        }

        private void button_machine_alias_up_Click(object sender, EventArgs e)
        {
            int up = listBox_machine_alias.SelectedIndex;
            if (up > 0)
            {
                int before = up - 1;
                string upstr = this.config.Config.MachineAlias[up];
                string beforestr = this.config.Config.MachineAlias[before];

                string[] temp = new string[] { };
                for (int i = 0; i < listBox_machine_alias.Items.Count; i++)
                {
                    if (i == before)
                    {
                        temp = temp.Concat(new string[] { upstr }).ToArray();
                    }
                    else if (i == up)
                    {
                        temp = temp.Concat(new string[] { beforestr }).ToArray();
                    }
                    else
                    {
                        temp = temp.Concat(new string[] { this.config.Config.MachineAlias[i] }).ToArray();
                    }
                }
                this.config.Config.MachineAlias = temp;
                this.refreshlist_machine_alias();
                this.listBox_machine_alias.SelectedIndex = up - 1;
            }
        }

        private void button_machine_alias_down_Click(object sender, EventArgs e)
        {
            int down = listBox_machine_alias.SelectedIndex;
            if (down >= 0 && down < listBox_machine_alias.Items.Count)
            {
                int after = down + 1;
                string downstr = this.config.Config.MachineAlias[down];
                string afterstr = this.config.Config.MachineAlias[after];

                string[] temp = new string[] { };
                for (int i = 0; i < listBox_machine_alias.Items.Count; i++)
                {
                    if (i == after)
                    {
                        temp = temp.Concat(new string[] { downstr }).ToArray();
                    }
                    else if (i == down)
                    {
                        temp = temp.Concat(new string[] { afterstr }).ToArray();
                    }
                    else
                    {
                        temp = temp.Concat(new string[] { this.config.Config.MachineAlias[i] }).ToArray();
                    }
                }
                this.config.Config.MachineAlias = temp;
                this.refreshlist_machine_alias();
                this.listBox_machine_alias.SelectedIndex = down + 1;
            }
        }

        private void button_machine_alias_add_Click(object sender, EventArgs e)
        {
            if (this.config.Config.MachineAlias != null)
            {
                foreach (string item in this.config.Config.MachineAlias)
                {
                    if (item.StartsWith(textBox_machine_alias.Text + "="))
                    {
                        MessageBox.Show("Alias " + textBox_machine_alias.Text + " exists!");
                        return;
                    }
                }
            }
            else
            {
                this.config.Config.MachineAlias = new string[] { };
            }
            int si = listBox_machine_alias.Items.Count;
            this.config.Config.MachineAlias = this.config.Config.MachineAlias.Concat(
                new string[] { string.Format("{0}={1}:{2}",
                    textBox_machine_alias.Text,
                    textBox_machine_alias_ip.Text,
                    textBox_machine_alias_port.Text) }
                ).ToArray();
            this.refreshlist_machine_alias();
            this.listBox_machine_alias.SelectedIndex = si;
        }

        private void checkBox_Machine_Alias_CheckedChanged(object sender, EventArgs e)
        {
            groupBox_machine_alias.Enabled = checkBox_Machine_Alias.Checked;
        }

        private void button_machine_alias_edit_Click(object sender, EventArgs e)
        {
            int edit = listBox_machine_alias.SelectedIndex;
            if (edit >= 0)
            {
                for (int i = 0; i < config.Config.MachineAlias.Count(); i++)
                {
                    if (i == edit)
                    {
                        continue;
                    }

                    if (config.Config.MachineAlias[i].StartsWith(textBox_machine_alias.Text + "="))
                    {
                        MessageBox.Show("Alias " + textBox_machine_alias.Text + " exists!");
                        return;
                    }
                }
                this.config.Config.MachineAlias.SetValue(string.Format("{0}={1}:{2}",
                        textBox_machine_alias.Text,
                        textBox_machine_alias_ip.Text,
                        textBox_machine_alias_port.Text), edit);
                this.refreshlist_machine_alias();
                this.listBox_machine_alias.SelectedIndex = edit;
            }
        }

        private void button_machine_alias_delete_Click(object sender, EventArgs e)
        {
            int delete = listBox_machine_alias.SelectedIndex;
            if (delete >= 0)
            {
                string[] temp = new string[] { };
                for (int i = 0; i < listBox_machine_alias.Items.Count; i++)
                {
                    if (i == delete)
                    {
                        continue;
                    }
                    temp = temp.Concat(new string[] { listBox_machine_alias.Items[i].ToString() }).ToArray();
                }
                this.config.Config.MachineAlias = temp;
                this.refreshlist_machine_alias();
                if (listBox_machine_alias.Items.Count > 0)
                {
                    this.listBox_machine_alias.SelectedIndex = delete - 1 < 0 ? 0 : delete - 1;
                }
            }
        }

        private void listBox_paralleltest_SelectedIndexChanged(object sender, EventArgs e)
        {
            int select = listBox_paralleltest.SelectedIndex;
            if (select < 0)
            {
                this.initparalleltest();                
            }
            else
            {
                button_paralleltest_up.Enabled =
                    button_paralleltest_down.Enabled =
                    button_paralleltest_delete.Enabled =
                    button_paralleltest_edit.Enabled =
                button_paralleltest_add.Enabled = true;
                this.initparalleltest(this.config.Config.ParallelTests[select]);
                if (select == 0)
                {
                    button_paralleltest_up.Enabled = false;
                }
                if (select == listBox_paralleltest.Items.Count - 1)
                {
                    button_paralleltest_down.Enabled = false;
                }
            }
        }

        private void initparalleltest()
        {
            button_paralleltest_up.Enabled =
                    button_paralleltest_down.Enabled =
                    button_paralleltest_delete.Enabled =
                    button_paralleltest_edit.Enabled = false;
            button_paralleltest_add.Enabled = true;
            textBox_parallelTest_name.Text = string.Empty;            
                checkBox_agents.Checked = 
                groupBox_agents.Enabled =
                checkBox_Parallet_Test_Timeout.Checked =
                checkBox_Barrier_Timeout.Checked =
                textBox_parallet_test_timeout.Enabled =
                textBox_barrier_timeout.Enabled = false;
                this.inittests();
        }

        private void inittests()
        {
            button_tests_up.Enabled =
                    button_tests_down.Enabled =
                    button_tests_delete.Enabled =
                    button_tests_edit.Enabled = false;
            button_tests_add.Enabled = true;
            textBox_tests_name.Text =
                textBox_tests_testtorun.Text =
                textBox_tests_assembly.Text =
                comboBox_test_machine.Text = string.Empty;
            checkBox_Wait_Barriers.Checked = groupBox_wait_barriers.Enabled =
                checkBox_Test_Params.Checked = groupBox_test_params.Enabled = false;
            this.initparams();
            this.initwaitbarriers();
        }

        private void refreshlist_waitbarrier(string[] barriers)
        {

            listBox_wait_barriers.Items.Clear();
            if (null != barriers)
            {
                if (barriers.Count() > 0)
                {
                    foreach (string barrier in barriers)
                    {
                        listBox_wait_barriers.Items.Add(barrier);
                    }
                }
            }
        }

        private void refreshlist_params(string[] param)
        {
            listBox_test_params.Items.Clear();
            if (null != param)
            {
                if (param.Count() > 0)
                {
                    foreach (string p in param)
                    {
                        listBox_test_params.Items.Add(p);
                    }
                }
            }
        }

        private void inittests(LauncherConfig.TestConf test)
        {
            textBox_tests_name.Text = test.Name;
            textBox_tests_assembly.Text = test.Assembly;
            textBox_tests_testtorun.Text = test.TestToRun;
            comboBox_test_machine.Text = test.Machine;
            if (test.WaitBarriers != null)
            {
                checkBox_Wait_Barriers.Checked =
                    groupBox_wait_barriers.Enabled = true;
                this.refreshlist_waitbarrier(test.WaitBarriers);
            }
            else
            {
                checkBox_Wait_Barriers.Checked =
                    groupBox_wait_barriers.Enabled = false;
                this.initwaitbarriers();
            }

            if (test.TestParams != null)
            {
                checkBox_Test_Params.Checked =
                    groupBox_test_params.Enabled = true;
                this.refreshlist_params(test.TestParams);
            }
            else
            {
                checkBox_Test_Params.Checked =
                   groupBox_test_params.Enabled = false;
                this.initparams();
            }
            button_tests_up.Enabled =
        button_tests_down.Enabled =
        button_tests_delete.Enabled =
        button_tests_edit.Enabled =
                button_tests_add.Enabled = true;
        }

        private void initparams()
        {
            listBox_test_params.Items.Clear();
            textBox_testparams.Text = string.Empty;
            button_testparams_up.Enabled =
                    button_testparams_down.Enabled =
                    button_testparamsdelete.Enabled =
                    button_testparamsedit.Enabled = false;
            button_testparams_add.Enabled = true;
        }

        private void initwaitbarriers()
        {
            listBox_wait_barriers.Items.Clear();
            button_waitbarriers_up.Enabled =
                    button_waitbarriers_down.Enabled =
                    button_waitbarriers_delete.Enabled =
                    button_waitbarriers_edit.Enabled = false;
            button_waitbarriers_add.Enabled = true;
            textBox_waitbarriers.Text = string.Empty;
        }

        private void initwaitbarriers(string barrier)
        {
            button_waitbarriers_up.Enabled =
                    button_waitbarriers_down.Enabled =
                    button_waitbarriers_delete.Enabled =
                    button_waitbarriers_edit.Enabled =
            button_waitbarriers_add.Enabled = true;
            textBox_waitbarriers.Text = barrier;
        }

        private void initparams(string para)
        {
            textBox_testparams.Text = para;
            button_testparams_up.Enabled =
                    button_testparams_down.Enabled =
                    button_testparamsdelete.Enabled =
                    button_testparamsedit.Enabled = 
            button_testparams_add.Enabled = true;
        }

        private void initparalleltest(LauncherConfig.ParallelTest paralleltest)
        {
            textBox_parallelTest_name.Text = paralleltest.Name;
            if (paralleltest.Agents != null)
            {
                checkBox_agents.Checked=
                    groupBox_agents.Enabled= true;
                this.refreshlist_agent(paralleltest.Agents);
            }
            else
            {
                checkBox_agents.Checked = groupBox_agents.Enabled = false;
            }

            if (paralleltest.ParalletTestTimeout == 0)
            {
                checkBox_Parallet_Test_Timeout.Checked =textBox_parallet_test_timeout.Enabled= false;
            }
            else
            {
                checkBox_Parallet_Test_Timeout.Checked = textBox_parallet_test_timeout.Enabled = true;
                textBox_parallet_test_timeout.Text = paralleltest.ParalletTestTimeout.ToString();
            }

            if (paralleltest.BarrierTimeout == 0)
            {
                checkBox_Barrier_Timeout.Checked = textBox_barrier_timeout.Enabled = false;
            }
            else
            {
                checkBox_Barrier_Timeout.Checked = textBox_barrier_timeout.Enabled = true;
                textBox_barrier_timeout.Text = paralleltest.BarrierTimeout.ToString();
            }
            if (paralleltest.Tests == null)
            {
                listBox_tests.Items.Clear();
                this.inittests();
            }
            else
            {
                refrestlist_tests(paralleltest.Tests);
            }

            if (paralleltest.Agents == null)
            {
                listBox_agents.Items.Clear();
                this.initagents();
            }
            else
            {
                refreshlist_agent(paralleltest.Agents);
            }
        }       

        private void refrestlist_tests(LauncherConfig.TestConf[] tests)
        {
            listBox_tests.Items.Clear();
            if (null != tests)
            {
                if (tests.Count() > 0)
                {
                    foreach (LauncherConfig.TestConf test in tests)
                    {
                        listBox_tests.Items.Add(test.Name);
                    }
                }
            }
        }

        private void refreshlist_agent(string[] agents)
        {
            listBox_agents.Items.Clear();
            if (null != agents)
            {
                if (agents.Count() > 0)
                {
                    foreach (string agent in agents)
                    {
                        listBox_agents.Items.Add(agent);
                    }
                }
            }
        }

        private void button_paralleltest_up_Click(object sender, EventArgs e)
        {
            int up = listBox_paralleltest.SelectedIndex;
            if (up > 0)
            {
                int before = up - 1;
                LauncherConfig.ParallelTest upstr = this.config.Config.ParallelTests[up];
                LauncherConfig.ParallelTest beforestr = this.config.Config.ParallelTests[before];

                LauncherConfig.ParallelTest[] temp = new LauncherConfig.ParallelTest[] { };
                for (int i = 0; i < listBox_paralleltest.Items.Count; i++)
                {
                    if (i == before)
                    {
                        temp = temp.Concat(new LauncherConfig.ParallelTest[] { upstr }).ToArray();
                    }
                    else if (i == up)
                    {
                        temp = temp.Concat(new LauncherConfig.ParallelTest[] { beforestr }).ToArray();
                    }
                    else
                    {
                        temp = temp.Concat(new LauncherConfig.ParallelTest[] { this.config.Config.ParallelTests[i] }).ToArray();
                    }
                }
                this.config.Config.ParallelTests= temp;
                this.refreshlist_ParallelTests();
                this.listBox_paralleltest.SelectedIndex = up - 1;
            }
        }

        private void button_paralleltest_down_Click(object sender, EventArgs e)
        {
            int down = listBox_paralleltest.SelectedIndex;
            if (down >= 0 && down< listBox_paralleltest.Items.Count)
            {
                int after = down + 1;
                LauncherConfig.ParallelTest upstr = this.config.Config.ParallelTests[down];
                LauncherConfig.ParallelTest beforestr = this.config.Config.ParallelTests[after];

                LauncherConfig.ParallelTest[] temp = new LauncherConfig.ParallelTest[] { };
                for (int i = 0; i < listBox_paralleltest.Items.Count; i++)
                {
                    if (i == after)
                    {
                        temp = temp.Concat(new LauncherConfig.ParallelTest[] { upstr }).ToArray();
                    }
                    else if (i == down)
                    {
                        temp = temp.Concat(new LauncherConfig.ParallelTest[] { beforestr }).ToArray();
                    }
                    else
                    {
                        temp = temp.Concat(new LauncherConfig.ParallelTest[] { this.config.Config.ParallelTests[i] }).ToArray();
                    }
                }
                this.config.Config.ParallelTests = temp;
                this.refreshlist_ParallelTests();
                this.listBox_paralleltest.SelectedIndex = down+ 1;
            }
        }

        private void button_paralleltest_add_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox_parallelTest_name.Text))
            {
                MessageBox.Show("test name could not be null");
                return;
            }

            if (this.config.Config.ParallelTests == null)
            {
                this.config.Config.ParallelTests = new LauncherConfig.ParallelTest[] { };
            }

            int si = listBox_paralleltest.Items.Count;
            LauncherConfig.ParallelTest temptest = new LauncherConfig.ParallelTest();
            temptest.Name =textBox_parallelTest_name.Text;
            if (checkBox_Parallet_Test_Timeout.Checked)
            {
                temptest.ParalletTestTimeout=Convert.ToInt32(textBox_parallelTest_name.Text);
            }
            else
            {
                ;
            }

            if (checkBox_Barrier_Timeout.Checked)
            {
                temptest.BarrierTimeout= Convert.ToInt32(textBox_barrier_timeout.Text);
            }
            else
            {
                ;
            }

            if (checkBox_agents.Checked)
            {
                string[] temp = new string[] { };
                foreach (object item in listBox_agents.Items)
                {
                    temp = temp.Concat(new string[] { item.ToString() }).ToArray();
                }

                temptest.Agents = temp;
            }
            else
            {
                temptest.Agents = null;
            }

            this.config.Config.ParallelTests =
               this.config.Config.ParallelTests.Concat(
               new LauncherConfig.ParallelTest[]{temptest}).ToArray();
            this.refreshlist_ParallelTests();
            this.listBox_paralleltest.SelectedIndex = si;
        }

        private void button_paralleltest_delete_Click(object sender, EventArgs e)
        {
            int delete = listBox_paralleltest.SelectedIndex;
            if (delete >= 0)
            {
                LauncherConfig.ParallelTest[] temp = new LauncherConfig.ParallelTest[] { };
                for (int i = 0; i < listBox_paralleltest.Items.Count; i++)
                {
                    if (i == delete)
                    {
                        continue;
                    }
                    temp = temp.Concat(new LauncherConfig.ParallelTest[] { this.config.Config.ParallelTests[i] }).ToArray();
                }
                this.config.Config.ParallelTests = temp;
                this.refreshlist_ParallelTests();
                if (listBox_paralleltest.Items.Count > 0)
                {
                    this.listBox_paralleltest.SelectedIndex = delete - 1 < 0 ? 0 : delete - 1;
                }
            }
        }

        private void button_paralleltest_edit_Click(object sender, EventArgs e)
        {
            int edit = listBox_paralleltest.SelectedIndex;
            if (edit >= 0)
            {

                this.config.Config.ParallelTests[edit].Name = textBox_parallelTest_name.Text;
                if (checkBox_Parallet_Test_Timeout.Checked)
                {
                    this.config.Config.ParallelTests[edit].ParalletTestTimeout = Convert.ToInt32(textBox_parallelTest_name.Text);
                }
                else
                {
                    this.config.Config.ParallelTests[edit].ParalletTestTimeout = 0;
                }

                if (checkBox_Barrier_Timeout.Checked)
                {
                    this.config.Config.ParallelTests[edit].BarrierTimeout = Convert.ToInt32(textBox_barrier_timeout.Text);
                }
                else
                {
                    this.config.Config.ParallelTests[edit].BarrierTimeout = 0;
                }

                if (checkBox_agents.Checked)
                {
                    string[] temp = new string[] { };
                    foreach (object item in listBox_agents.Items)
                    {
                        temp = temp.Concat(new string[] { item.ToString() }).ToArray();
                    }

                    this.config.Config.ParallelTests[edit].Agents = temp;
                }
                else
                {
                    this.config.Config.ParallelTests[edit].Agents = null;
                }

                this.refreshlist_ParallelTests();
                this.listBox_paralleltest.SelectedIndex = edit;
            }
        }

        private void checkBox_Parallet_Test_Timeout_CheckedChanged(object sender, EventArgs e)
        {
            textBox_parallet_test_timeout.Enabled = checkBox_Parallet_Test_Timeout.Checked;
        }

        private void checkBox_Barrier_Timeout_CheckedChanged(object sender, EventArgs e)
        {
            textBox_barrier_timeout.Enabled = checkBox_Barrier_Timeout.Checked;
        }

        private void checkBox_agents_CheckedChanged(object sender, EventArgs e)
        {
            groupBox_agents.Enabled = checkBox_agents.Checked;
        }

        private void button_agents_up_Click(object sender, EventArgs e)
        {
            int up = listBox_agents.SelectedIndex;
            if (up > 0)
            {
                int before = up - 1;
                string upstr = this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Agents[up];
                string beforestr = this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Agents[before];

                string[] temp = new string[] { };
                for (int i = 0; i < listBox_agents.Items.Count; i++)
                {
                    if (i == before)
                    {
                        temp = temp.Concat(new string[] { upstr }).ToArray();
                    }
                    else if (i == up)
                    {
                        temp = temp.Concat(new string[] { beforestr }).ToArray();
                    }
                    else
                    {
                        temp = temp.Concat(new string[] { this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Agents[i] }).ToArray();
                    }
                }
                this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Agents = temp;
                this.refreshlist_agent(this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Agents);
                this.listBox_agents.SelectedIndex = up - 1;
            }
        }

        private void button_agents_down_Click(object sender, EventArgs e)
        {
            int down = listBox_agents.SelectedIndex;
            if (down >= 0  && down < listBox_agents.Items.Count)
            {
                int after = down +1;
                string upstr = this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Agents[down];
                string beforestr = this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Agents[after];

                string[] temp = new string[] { };
                for (int i = 0; i < listBox_agents.Items.Count; i++)
                {
                    if (i == after)
                    {
                        temp = temp.Concat(new string[] { upstr }).ToArray();
                    }
                    else if (i == down)
                    {
                        temp = temp.Concat(new string[] { beforestr }).ToArray();
                    }
                    else
                    {
                        temp = temp.Concat(new string[] { this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Agents[i] }).ToArray();
                    }
                }
                this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Agents = temp;
                this.refreshlist_agent(this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Agents);
                this.listBox_agents.SelectedIndex = down + 1;
            }
        }

        private void button_agents_add_Click(object sender, EventArgs e)
        {
            if (listBox_paralleltest.SelectedIndex < 0)
            {
                return;
            }
            if (string.IsNullOrEmpty(textBox_agents_port.Text) ||string.IsNullOrEmpty(textBox_agents_ip.Text) )
            {
                MessageBox.Show("ip or port could not be null");
                return;
            }
            if (this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Agents == null)
            {
                this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Agents = new string[] { };
            }
            int si = listBox_agents.Items.Count;
            this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Agents =
                this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Agents.Concat(
                new string[] { string.Format("{0}:{1}",textBox_agents_ip.Text,textBox_agents_port.Text) }).ToArray();
            this.refreshlist_agent(this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Agents);
            this.listBox_agents.SelectedIndex = si;
        }

        private void button_agents_delete_Click(object sender, EventArgs e)
        {
            int delete = listBox_agents.SelectedIndex;
            if (delete >= 0)
            {
                string[] temp = new string[] { };
                for (int i = 0; i < listBox_agents.Items.Count; i++)
                {
                    if (i == delete)
                    {
                        continue;
                    }
                    temp = temp.Concat(new string[] { listBox_agents.Items[i].ToString() }).ToArray();
                }
                this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Agents = temp;
                this.refreshlist_agent(this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Agents);
                if (listBox_agents.Items.Count > 0)
                {
                    this.listBox_agents.SelectedIndex = delete - 1 < 0 ? 0 : delete - 1;
                }
            }
        }

        private void button_agents_edit_Click(object sender, EventArgs e)
        {
            int edit = listBox_agents.SelectedIndex;
            if (edit >= 0)
            {
                if (string.IsNullOrEmpty(textBox_agents_port.Text) || string.IsNullOrEmpty(textBox_agents_ip.Text))
                {
                    MessageBox.Show("ip or port could not be null");
                    return;
                }
                this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Agents.SetValue(
                    string.Format("{0}:{1}", textBox_agents_ip.Text, textBox_agents_port.Text), edit);
                this.refreshlist_agent(this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Agents);
                this.listBox_agents.SelectedIndex = edit;
            }
        }

        private void listBox_agents_SelectedIndexChanged(object sender, EventArgs e)
        {
            int select = listBox_agents.SelectedIndex;
            if (select < 0)
            {
                this.initagents();
            }
            else
            {
                this.initagents(this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Agents[select]);
                if (select == 0)
                {
                    button_agents_up.Enabled = false;
                }
                if (select == listBox_agents.Items.Count - 1)
                {
                    button_agents_down.Enabled = false;
                }
            }
        }

        private void initagents(string agent)
        {
            button_agents_up.Enabled =
                    button_agents_down.Enabled =
                    button_agents_delete.Enabled =
                    button_agents_edit.Enabled =
            button_agents_add.Enabled = true;
            textBox_agents_ip.Text = agent.Split(':')[0];
            textBox_agents_port.Text = agent.Split(':')[1];
        }

        private void initagents()
        {
            button_agents_up.Enabled =
                    button_agents_down.Enabled =
                    button_agents_delete.Enabled =
                    button_agents_edit.Enabled = false;
            button_agents_add.Enabled = true;
            textBox_agents_ip.Text =
                textBox_agents_port.Text = string.Empty;
        }

        private void checkBox_Test_Params_CheckedChanged(object sender, EventArgs e)
        {
            groupBox_test_params.Enabled = checkBox_Test_Params.Checked;
        }

        private void checkBox_Wait_Barriers_CheckedChanged(object sender, EventArgs e)
        {
            groupBox_wait_barriers.Enabled = checkBox_Wait_Barriers.Checked;
        }

        private void listBox_test_params_SelectedIndexChanged(object sender, EventArgs e)
        {
            int select = listBox_test_params.SelectedIndex;
            if (select < 0)
            {
                this.initparams();
            }
            else
            {
                this.initparams(this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests[listBox_tests.SelectedIndex].TestParams[select]);
                if (select == 0)
                {
                    button_testparams_up.Enabled = false;
                }
                if (select == listBox_test_params.Items.Count - 1)
                {
                    button_testparams_down.Enabled = false;
                }
            }
        }

        private void listBox_wait_barriers_SelectedIndexChanged(object sender, EventArgs e)
        {
            int select = listBox_wait_barriers.SelectedIndex;
            if (select < 0)
            {
                this.initwaitbarriers();
            }
            else
            {
                this.initwaitbarriers(this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests[listBox_tests.SelectedIndex].WaitBarriers[select]);
                if (select == 0)
                {
                    button_waitbarriers_up.Enabled = false;
                }
                if (select == listBox_wait_barriers.Items.Count - 1)
                {
                    button_waitbarriers_down.Enabled = false;
                }
            }
        }

        private void radioButton_version_n_CheckedChanged(object sender, EventArgs e)
        {
            checkBox_Variables.Checked = groupBox_variables.Enabled =
                checkBox_Launcher_Machine.Checked = groupBox_launcher_machine.Enabled =
                checkBox_Result_File_Name.Checked = groupBox_result_File_name.Enabled =
                checkBox_Machine_Alias.Checked = groupBox_machine_alias.Enabled =
                checkBox_Parallet_Test_Timeout.Checked = textBox_parallet_test_timeout.Enabled =
                checkBox_Barrier_Timeout.Checked = textBox_barrier_timeout.Enabled =
                checkBox_Wait_Barriers.Checked = groupBox_wait_barriers.Enabled = false;
        }

        private void radioButton_version_p_CheckedChanged(object sender, EventArgs e)
        {
            checkBox_Launcher_Machine.Checked = groupBox_launcher_machine.Enabled = true;
            checkBox_Variables.Checked = groupBox_variables.Enabled =
               checkBox_Result_File_Name.Checked = groupBox_result_File_name.Enabled =
               checkBox_Machine_Alias.Checked = groupBox_machine_alias.Enabled =
               checkBox_Parallet_Test_Timeout.Checked = textBox_parallet_test_timeout.Enabled =
               checkBox_Barrier_Timeout.Checked = textBox_barrier_timeout.Enabled =
               checkBox_Wait_Barriers.Checked = groupBox_wait_barriers.Enabled = false;
        }

        private void radioButton_version_Q_CheckedChanged(object sender, EventArgs e)
        {
            checkBox_Variables.Checked = groupBox_variables.Enabled = false;
                checkBox_Launcher_Machine.Checked = groupBox_launcher_machine.Enabled =
                checkBox_Result_File_Name.Checked = groupBox_result_File_name.Enabled =
                checkBox_Machine_Alias.Checked = groupBox_machine_alias.Enabled =
                checkBox_Parallet_Test_Timeout.Checked = textBox_parallet_test_timeout.Enabled =
                checkBox_Barrier_Timeout.Checked = textBox_barrier_timeout.Enabled =
                checkBox_Wait_Barriers.Checked = groupBox_wait_barriers.Enabled = true;
        }

        private void button_waitbarriers_edit_Click(object sender, EventArgs e)
        {
            int edit = listBox_wait_barriers.SelectedIndex;
            if (edit >= 0)
            {
                if (string.IsNullOrEmpty(textBox_waitbarriers.Text))
                {
                    MessageBox.Show("wait barrier could not be null");
                    return;
                }
                this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests[listBox_tests.SelectedIndex].WaitBarriers.SetValue(textBox_waitbarriers.Text, edit);
                this.refreshlist_waitbarrier(this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests[listBox_tests.SelectedIndex].WaitBarriers);
                this.listBox_wait_barriers.SelectedIndex = edit;
            }
        }

        private void button_waitbarriers_delete_Click(object sender, EventArgs e)
        {
            int delete = listBox_wait_barriers.SelectedIndex;
            if (delete >= 0)
            {
                string[] temp = new string[] { };
                for (int i = 0; i < listBox_wait_barriers.Items.Count; i++)
                {
                    if (i == delete)
                    {
                        continue;
                    }
                    temp = temp.Concat(new string[] { listBox_wait_barriers.Items[i].ToString() }).ToArray();
                }
                this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests[listBox_tests.SelectedIndex].WaitBarriers = temp;
                this.refreshlist_waitbarrier(this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests[listBox_tests.SelectedIndex].WaitBarriers);

                if (listBox_wait_barriers.Items.Count > 0)
                {
                    this.listBox_wait_barriers.SelectedIndex = delete - 1 < 0 ? 0 : delete - 1;
                }
            }
        }

        private void button_waitbarriers_add_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox_waitbarriers.Text))
            {
                MessageBox.Show("wait barrier could not be null");
                return;
            }
            if (this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests[listBox_tests.SelectedIndex].WaitBarriers == null)
            {
                this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests[listBox_tests.SelectedIndex].WaitBarriers = new string[] { };
            }
            int si = listBox_wait_barriers.Items.Count;
            this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests[listBox_tests.SelectedIndex].WaitBarriers =
                this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests[listBox_tests.SelectedIndex].WaitBarriers.Concat(
                new string[] { textBox_waitbarriers.Text }).ToArray();
            this.refreshlist_waitbarrier(this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests[listBox_tests.SelectedIndex].WaitBarriers);
            this.listBox_wait_barriers.SelectedIndex = si;
        }

        private void button_waitbarriers_down_Click(object sender, EventArgs e)
        {
            int down = listBox_wait_barriers.SelectedIndex;
            if (down >= 0 && down < listBox_wait_barriers.Items.Count)
            {
                int after = down + 1;
                string upstr = this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests[listBox_tests.SelectedIndex].WaitBarriers[down];
                string beforestr = this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests[listBox_tests.SelectedIndex].WaitBarriers[after];

                string[] temp = new string[] { };
                for (int i = 0; i < listBox_wait_barriers.Items.Count; i++)
                {
                    if (i == after)
                    {
                        temp = temp.Concat(new string[] { upstr }).ToArray();
                    }
                    else if (i == down)
                    {
                        temp = temp.Concat(new string[] { beforestr }).ToArray();
                    }
                    else
                    {
                        temp = temp.Concat(new string[] { this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests[listBox_tests.SelectedIndex].WaitBarriers[i] }).ToArray();
                    }
                }
                this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests[listBox_tests.SelectedIndex].WaitBarriers = temp;
                this.refreshlist_waitbarrier(this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests[listBox_tests.SelectedIndex].WaitBarriers);
                this.listBox_wait_barriers.SelectedIndex = down + 1;
            }
        }

        private void button_waitbarriers_up_Click(object sender, EventArgs e)
        {
            int up = listBox_wait_barriers.SelectedIndex;
            if (up > 0)
            {
                int before = up - 1;
                string upstr = this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests[listBox_tests.SelectedIndex].WaitBarriers[up];
                string beforestr = this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests[listBox_tests.SelectedIndex].WaitBarriers[before];

                string[] temp = new string[] { };
                for (int i = 0; i < listBox_wait_barriers.Items.Count; i++)
                {
                    if (i == before)
                    {
                        temp = temp.Concat(new string[] { upstr }).ToArray();
                    }
                    else if (i == up)
                    {
                        temp = temp.Concat(new string[] { beforestr }).ToArray();
                    }
                    else
                    {
                        temp = temp.Concat(new string[] { this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests[listBox_tests.SelectedIndex].WaitBarriers[i] }).ToArray();
                    }
                }
                this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests[listBox_tests.SelectedIndex].WaitBarriers = temp;
                this.refreshlist_waitbarrier(this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests[listBox_tests.SelectedIndex].WaitBarriers);
                this.listBox_wait_barriers.SelectedIndex = up - 1;
            }
        }

        private void button_testparamsdelete_Click(object sender, EventArgs e)
        {
            int delete = listBox_test_params.SelectedIndex;
            if (delete >= 0)
            {
                string[] temp = new string[] { };
                for (int i = 0; i < listBox_test_params.Items.Count; i++)
                {
                    if (i == delete)
                    {
                        continue;
                    }
                    temp = temp.Concat(new string[] { listBox_test_params.Items[i].ToString() }).ToArray();
                }
                this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests[listBox_tests.SelectedIndex].TestParams = temp;
                this.refreshlist_params(this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests[listBox_tests.SelectedIndex].TestParams);

                if (listBox_test_params.Items.Count > 0)
                {
                    this.listBox_test_params.SelectedIndex = delete - 1 < 0 ? 0 : delete - 1;
                }
            }
        }

        private void button_testparams_add_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox_testparams.Text))
            {
                MessageBox.Show("test param could not be null");
                return;
            }
            if (this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests[listBox_tests.SelectedIndex].TestParams == null)
            {
                this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests[listBox_tests.SelectedIndex].TestParams = new string[] { };
            }
            int si =listBox_test_params.Items.Count;
            this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests[listBox_tests.SelectedIndex].TestParams =
                this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests[listBox_tests.SelectedIndex].TestParams.Concat(
                new string[] { textBox_testparams.Text}).ToArray();
            this.refreshlist_params(this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests[listBox_tests.SelectedIndex].TestParams);
            this.listBox_test_params.SelectedIndex = si;
        }

        private void button_testparams_down_Click(object sender, EventArgs e)
        {
            int down = listBox_test_params.SelectedIndex;
            if (down >= 0 && down < listBox_test_params.Items.Count)
            {
                int after = down + 1;
                string upstr = this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests[listBox_tests.SelectedIndex].TestParams[down];
                string beforestr = this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests[listBox_tests.SelectedIndex].TestParams[after];

                string[] temp = new string[] { };
                for (int i = 0; i < listBox_test_params.Items.Count; i++)
                {
                    if (i == after)
                    {
                        temp = temp.Concat(new string[] { upstr }).ToArray();
                    }
                    else if (i == down)
                    {
                        temp = temp.Concat(new string[] { beforestr }).ToArray();
                    }
                    else
                    {
                        temp = temp.Concat(new string[] { this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests[listBox_tests.SelectedIndex].TestParams[i] }).ToArray();
                    }
                }
                this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests[listBox_tests.SelectedIndex].TestParams = temp;
                this.refreshlist_params(this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests[listBox_tests.SelectedIndex].TestParams);
                this.listBox_test_params.SelectedIndex = down + 1;
            }
        }

        private void button_testparams_up_Click(object sender, EventArgs e)
        {            
            int up = listBox_test_params.SelectedIndex;
            if (up > 0)
            {
                int before = up - 1;
                string upstr = this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests[listBox_tests.SelectedIndex].TestParams[up];
                string beforestr = this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests[listBox_tests.SelectedIndex].TestParams[before];

                string[] temp = new string[] { };
                for (int i = 0; i < listBox_test_params.Items.Count; i++)
                {
                    if (i == before)
                    {
                        temp = temp.Concat(new string[] { upstr }).ToArray();
                    }
                    else if (i == up)
                    {
                        temp = temp.Concat(new string[] { beforestr }).ToArray();
                    }
                    else
                    {
                        temp = temp.Concat(new string[] { this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests[listBox_tests.SelectedIndex].TestParams[i] }).ToArray();
                    }
                }
                this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests[listBox_tests.SelectedIndex].TestParams = temp;
                this.refreshlist_params(this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests[listBox_tests.SelectedIndex].TestParams);
                this.listBox_test_params.SelectedIndex = up - 1;
            }
        }

        private void button_testparamsedit_Click(object sender, EventArgs e)
        {
            int edit = listBox_test_params.SelectedIndex;
            if (edit >= 0)
            {
                if (string.IsNullOrEmpty(textBox_testparams.Text))
                {
                    MessageBox.Show("test param could not be null");
                    return;
                }
                this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests[listBox_tests.SelectedIndex].TestParams.SetValue(textBox_testparams.Text,edit);
                this.refreshlist_params(this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests[listBox_tests.SelectedIndex].TestParams);
                this.listBox_test_params.SelectedIndex = edit;
            }
        }

        private void listBox_tests_SelectedIndexChanged(object sender, EventArgs e)
        {
            int select = listBox_tests.SelectedIndex;
            if (select < 0)
            {
                this.inittests();
            }
            else
            {
                this.inittests(this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests[select]);                
                if (select == 0)
                {
                    button_tests_up.Enabled = false;
                }
                if (select == listBox_tests.Items.Count - 1)
                {
                    button_tests_down.Enabled = false;
                }
            }
        }

        private void refreshcom_machine()
        {
            comboBox_test_machine.Items.Clear();
            if (checkBox_Machine_Alias.Checked)
            {
                foreach (object item in listBox_machine_alias.Items)
                {
                    comboBox_test_machine.Items.Add(item.ToString().Split('=')[0]);
                }
            }

            if (checkBox_Variables.Checked)
            {
                foreach (object item in listBox_variables.Items)
                {
                    comboBox_test_machine.Items.Add("$"+item.ToString()+":");
                }
            }

            if (checkBox_agents.Checked)
            {
                for (int i=0;i<listBox_agents.Items.Count;i++)
                {
                    comboBox_test_machine.Items.Add("_AGENT"+i.ToString());
                }
            }
        }

        private void button_tests_up_Click(object sender, EventArgs e)
        {
            int up = listBox_tests.SelectedIndex;
            if (up > 0)
            {
                int before = up - 1;
                LauncherConfig.TestConf upstr = this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests[up];
                LauncherConfig.TestConf beforestr = this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests[before];

                LauncherConfig.TestConf[] temp = new LauncherConfig.TestConf[] { };
                for (int i = 0; i < listBox_tests.Items.Count; i++)
                {
                    if (i == before)
                    {
                        temp = temp.Concat(new LauncherConfig.TestConf[] { upstr }).ToArray();
                    }
                    else if (i == up)
                    {
                        temp = temp.Concat(new LauncherConfig.TestConf[] { beforestr }).ToArray();
                    }
                    else
                    {
                        temp = temp.Concat(new LauncherConfig.TestConf[] { this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests[i] }).ToArray();
                    }
                }
                this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests = temp;
                this.refrestlist_tests(this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests);
                this.listBox_tests.SelectedIndex = up - 1;
            }
        }

        private void button_tests_edit_Click(object sender, EventArgs e)
        {
            int edit = listBox_tests.SelectedIndex;
            if (edit >= 0)
            {
                this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests[edit].Name = textBox_tests_name.Text;
                this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests[edit].Assembly =textBox_tests_assembly.Text;
                this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests[edit].TestToRun =textBox_tests_testtorun.Text;
                this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests[edit].Machine =comboBox_test_machine.Text;
                if (checkBox_Test_Params.Checked)
                {
                    string [] temp=new string[]{};
                    foreach(object item in listBox_test_params.Items)
                    {
                        temp=temp.Concat(new string[]{item.ToString()}).ToArray();
                    }

                    this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests[edit].TestParams = temp;
                }
                else
                {
                    this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests[edit].TestParams = null;
                }

                if (checkBox_Wait_Barriers.Checked)
                {
                    string[] temp = new string[] { };
                    foreach (object item in listBox_wait_barriers.Items)
                    {
                        temp = temp.Concat(new string[] { item.ToString() }).ToArray();
                    }

                    this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests[edit].WaitBarriers = temp;
                }
                else
                {
                    this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests[edit].WaitBarriers = null;
                }
                this.refrestlist_tests(this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests);
                this.listBox_tests.SelectedIndex = edit;
            }
        }

        private void button_tests_add_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox_tests_name.Text))
            {
                MessageBox.Show("test name could not be null");
                return;
            }

            if (this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests == null)
            {
                this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests = new LauncherConfig.TestConf[] { };
            }

            int si =listBox_tests.Items.Count;
            LauncherConfig.TestConf temptest = new LauncherConfig.TestConf();
            temptest.Name = textBox_tests_name.Text;
            temptest.Assembly = textBox_tests_assembly.Text;
            temptest.TestToRun = textBox_tests_testtorun.Text;
            temptest.Machine = comboBox_test_machine.Text;
            if (checkBox_Test_Params.Checked)
            {
                string[] temp = new string[] { };
                foreach (object item in listBox_test_params.Items)
                {
                    temp = temp.Concat(new string[] { item.ToString() }).ToArray();
                }

                temptest.TestParams = temp;
            }
            else
            {
                temptest.TestParams = null;
            }

            if (checkBox_Wait_Barriers.Checked)
            {
                string[] temp = new string[] { };
                foreach (object item in listBox_wait_barriers.Items)
                {
                    temp = temp.Concat(new string[] { item.ToString() }).ToArray();
                }

                temptest.WaitBarriers = temp;
            }
            else
            {
                temptest.WaitBarriers = null;
            }

            this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests =
                this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests.Concat(
                new LauncherConfig.TestConf[] { temptest }).ToArray();
            this.refrestlist_tests(this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests);
            this.listBox_tests.SelectedIndex = si;
        }

        private void button_tests_delete_Click(object sender, EventArgs e)
        {
            int delete = listBox_tests.SelectedIndex;
            if (delete >= 0)
            {
                LauncherConfig.TestConf[] temp = new LauncherConfig.TestConf[] { };
                for (int i = 0; i < listBox_tests.Items.Count; i++)
                {
                    if (i == delete)
                    {
                        continue;
                    }
                    temp = temp.Concat(new LauncherConfig.TestConf[] { this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests[i] }).ToArray();
                }
                this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests = temp;
                this.refrestlist_tests(this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests);

                if (listBox_tests.Items.Count > 0)
                {
                    this.listBox_tests.SelectedIndex = delete - 1 < 0 ? 0 : delete - 1;
                }
            }
        }

        private void button_tests_down_Click(object sender, EventArgs e)
        {
            int down = listBox_tests.SelectedIndex;
            if (down >= 0 && down < listBox_tests.Items.Count)
            {
                int after = down + 1;
                LauncherConfig.TestConf upstr = this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests[down];
                LauncherConfig.TestConf afterstr = this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests[after];

                LauncherConfig.TestConf[] temp = new LauncherConfig.TestConf[] { };
                for (int i = 0; i < listBox_tests.Items.Count; i++)
                {
                    if (i == after)
                    {
                        temp = temp.Concat(new LauncherConfig.TestConf[] { upstr }).ToArray();
                    }
                    else if (i == down)
                    {
                        temp = temp.Concat(new LauncherConfig.TestConf[] { afterstr }).ToArray();
                    }
                    else
                    {
                        temp = temp.Concat(new LauncherConfig.TestConf[] { this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests[i] }).ToArray();
                    }
                }
                this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests = temp;
                this.refrestlist_tests(this.config.Config.ParallelTests[listBox_paralleltest.SelectedIndex].Tests);
                this.listBox_tests.SelectedIndex = down + 1;
            }
        }
    }
}
